package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.EmployeeObjectQuery;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;



    @RequiresPermissions("employee:index")
    @PermissionName("员工主页")
    @RequestMapping("index")
    public String index() {
        return "employee/list";
    }


    //跳转到我的面板页面中
    @RequestMapping("panel")
    public String panel() {
        return "mypanel/list";
    }

    //进行密码修改
    @RequestMapping("changePassword")
    @ResponseBody
    public JsonResult changePassword(String oldOne , String newOne){
        JsonResult json = new JsonResult();
        try {
           json =  employeeService.changePassword(oldOne, newOne);
        }catch (Exception e){
            e.printStackTrace();
            json.markErrorMsg("更改密码失败");
        }
        return json;
    }


    @RequiresPermissions("employee:list")
    @PermissionName("员工查询")
    @RequestMapping("list")
    @ResponseBody
    public PageResult<Employee> list(Model model, @ModelAttribute("qo") EmployeeObjectQuery qo) {
        PageResult<Employee> list = employeeService.query(qo);
        return list;
    }
    @RequiresPermissions("employee:queryEmployee")
    @PermissionName("员工查询下拉框")
    @RequestMapping("queryEmployee")
    @ResponseBody
    public List<Employee> queryEmployee() {
        List<Employee> list = employeeService.getAll();
        return list;
    }

    @RequestMapping("validateUsername")
    @ResponseBody
    public boolean validateUsername(String username, Long id) {
        if (id == null) {
            if (employeeService.selectUsername(username) != null) {
                return false;
            }
        }
        return true;
    }

    @RequiresPermissions("employee:delete")
    @PermissionName("员工删除")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (id != null) {
                employeeService.delete(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("删除员工失败");
        }
        return jsonResult;
    }

    @RequiresPermissions("employee:changState")
    @PermissionName("员工复/离职状态更改")
    @RequestMapping("changState")
    @ResponseBody
    public JsonResult changState(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (id != null) {
                employeeService.changState(id);
            }

        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("更改状态失败");
        }
        return jsonResult;
    }


    @RequestMapping("batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (ids != null) {
                employeeService.batchDelete(ids);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("批量删除失败");
        }
        return jsonResult;
    }

    @RequiresPermissions("employee:saveOrUpdate")
    @PermissionName("员工创建更新")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Model model, Employee employee, Long[] roleIds) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (employee.getId() != null) {
                employeeService.update(employee, roleIds);
            } else {
                employeeService.save(employee, roleIds);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("保存或更新员工失败");
        }
        return jsonResult;
    }


    @RequestMapping("uploadImg")
    @ResponseBody
    public  Map<Object, Object> uploadImg(MultipartFile file){
        Map<Object, Object> path = employeeService.uploadHeadImg(file);
        return path;
    }

    @RequestMapping("getHeadImg")
    @ResponseBody
    public  Map<Object, Object> getHeadImg(){
        Map<Object, Object> path = employeeService.getHeadImg();
        return path;
    }

}
